package com.lenovo.smarttraffic.ui.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lenovo.smarttraffic.R;

/**
 * Created by Caleb.F
 * on 2020-03-12
 * at 12:04
 */
public class NetWorkActivity extends BaseActivity  {
    //1.5
    private Button mBtnGo;
    private EditText mEditText3;
    private EditText mEditText2;
    private EditText mEditText;
    private EditText mEditText4;
    private SharedPreferences.Editor mEditor;
    private static final String NETWORK_SETTING="com.lenovo.smarttraffic.ui.activity.NetWorkActivity";
    private static final String IP_ADDRESS="com.lenovo.smarttraffic.ui.activity.NetWorkActivity.ip";
    private static final String TAG = "NetWorkActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mEditor=getSharedPreferences(NETWORK_SETTING,MODE_PRIVATE).edit();
        initView();
    }

    private void initView() {
        initToolBar("网络设置");
        mBtnGo = findViewById(R.id.btn_go);
        mEditText3 = findViewById(R.id.editText3);
        mEditText2 = findViewById(R.id.editText2);
        mEditText = findViewById(R.id.editText);
        mEditText4 = findViewById(R.id.editText4);
        EditText[] mEditTexts= new EditText[]{
                mEditText3,
                mEditText2,
                mEditText,
                mEditText4
        };
        //1.6
        for (EditText editText : mEditTexts) {
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    editText.setSelection(s.toString().length());
                }

                @Override
                public void afterTextChanged(Editable s) {
                    String s1 = s.toString();
                    if(TextUtils.isEmpty(s1)){
                        return;
                    }
                    else{
                        if(Integer.parseInt(s1)>255){
                            s.delete(0,editText.getSelectionEnd());
                            Toast.makeText(NetWorkActivity.this, "输入有误，请重新输入", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });
        }
        mBtnGo.setOnClickListener(v->{
            String ipAddress = new StringBuilder().append(mEditText.getText()).append(".")
                    .append(mEditText2.getText()).append(".")
                    .append(mEditText3.getText()).append(".")
                    .append(mEditText4.getText()).toString();
            mEditor.putString(IP_ADDRESS,ipAddress).apply();
            //隐藏输入法
            LoginActivity.hideSoftInput(v);
            finish();
        });
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_network;
    }
    public static String getIP(Context context){
        return context.getSharedPreferences(NETWORK_SETTING, MODE_PRIVATE)
                .getString(IP_ADDRESS, "192.168.199.129");
    }

}
